/*
Copyright (C) 2015 Marien Raat

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <enemy.h>
#include <math.h>

#define PI 3.1415926535897932384

void Enemy::setEnemy(sf::Vector2f startPosition, sf::Vector2f velocity,
             float radius)
{
    this->position = startPosition;
    this->velocity = velocity;
    this->radius = radius;
    if (radius < 12)
        indexInTextures = 0;
    else if (radius < 20)
        indexInTextures = 1;
    else if (radius < 28)
        indexInTextures = 2;
    else if (radius < 42)
        indexInTextures = 3;
    else
        indexInTextures = 4;
}

void Enemy::update()
{
    position += velocity / (float) sqrt(radius);
}

void Enemy::draw(sf::RenderWindow *window,
                 sf::Texture *textures)
{
    enemySprite.setTexture(textures[indexInTextures]);
    enemySprite.setOrigin(100, 100);
    float scale = (radius * 2.5f) / 200.0f;
    enemySprite.setScale(scale, scale);
    enemySprite.setPosition(position);
    enemySprite.setRotation(atan2(velocity.y, velocity.x) / PI * 180.0);
    window->draw(enemySprite);
}
